package com.weiqiao.mall.service.impl;

import cn.hutool.core.util.IdUtil;
import com.google.gson.Gson;
import com.mongodb.client.model.Filters;
import com.weiqiao.mall.repository.IMemberConfigRepository;
import com.weiqiao.mall.service.IMemberConfigService;
import com.weiqiao.mall.service.dto.MemberConfigDTO;
import com.weiqiao.mall.service.vo.MemberConfigVO;
import com.weiqiao.utils.RedisUtil;
import com.weiqiao.utils.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("MemberConfigServiceImpl")
public class MemberConfigServiceImpl implements IMemberConfigService {

    @Autowired
    @Qualifier("MemberConfigRepositoryImpl")
    private IMemberConfigRepository memberConfigRepository;

    @Override
    public ResponseEntity getCompanyMemberConfig(int companyId) {
        //
        Bson filter = Filters.eq("company_id", companyId);
        String info = memberConfigRepository.getMemberConfigInfo(filter);
        Map map = new HashMap<>(1);
        map.put("result", this.judgeMemberGrade(companyId, info));
        return new ResponseEntity(map, HttpStatus.OK);
    }

    // 判断是否存在会员等级，若不存在，则一次性全部新建好会员等级（10级）
    private MemberConfigDTO judgeMemberGrade(int companyId, String info) {
        MemberConfigDTO entity;
        if (StringUtils.isBlank(info)) {
            this.updateMemberGrade(companyId);
            Bson filter = Filters.eq("company_id", companyId);
            String result = memberConfigRepository.getMemberConfigInfo(filter);
            entity = new Gson().fromJson(result, MemberConfigDTO.class);
        } else {
            entity = new Gson().fromJson(info, MemberConfigDTO.class);
        }
        return entity;
    }

    // 查询当前公司会员设置不存在时，自动创建会员设置信息。更新会员等级
    private void updateMemberGrade(int companyId) {
        MemberConfigVO entity = new MemberConfigVO();
        entity.setCompany_id(companyId);
        entity.setCreate_date(String.valueOf(System.currentTimeMillis()));
        for (int i = 1; i < 11; i++) {
            MemberConfigVO.MemberGradeEntity info = new MemberConfigVO.MemberGradeEntity();
            info.setUuid(IdUtil.simpleUUID());
            info.setGrade("VIP" + i);
            entity.getGrade_list().add(info);
        }
        this.updateCompanyMemberConfig(entity);
    }

    @Override
    public Boolean updateCompanyMemberConfig(MemberConfigVO entity) {
        Bson filter = Filters.eq("company_id", entity.getCompany_id());
        // 更新存Redis
        RedisUtil.hset("member_config", String.valueOf(entity.getCompany_id()), entity);
        // 存数据库
        return memberConfigRepository.updateMemberConfig(filter, Document.parse(new Gson().toJson(entity)));
    }
}
